package com.coderman.tianhua.idgenerator.core.listener;

import com.coderman.tianhua.idgenerator.core.buffer.ProductIdBuffer;
import com.coderman.tianhua.idgenerator.core.service.IdGeneratorFactory;
import com.coderman.tianhua.idgenerator.core.service.rule.SequenceRuleCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Description:
 * date: 2020/10/28
 *
 * @author fanchunshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Order(value = 100)
@Component
@Slf4j
public class MetricListener implements ApplicationListener<ApplicationStartedEvent> {

    @Autowired
    private ProductIdBuffer productIdBuffer;
    @Autowired
    private IdGeneratorFactory idGeneratorFactory;
    @Autowired
    private SequenceRuleCache sequenceRuleCache;
    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        log.info("ID生成器启动监听器开始 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>tianhua.idgenerator");

        //顺序不可调换
        sequenceRuleCache.load();
        productIdBuffer.init();
        idGeneratorFactory.start();
        log.info("ID生成器启动监听器结束 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>tianhua.idgenerator");

    }
}
